package edu.zju.gis.dbfg.fileserver.entity;

import lombok.Getter;
import lombok.Setter;
import org.json.JSONObject;

@Getter
@Setter
public class ConstructedDataField {
    private String name;
    private String text;
    private String type;
    private int length;
    private String description;
    private Boolean isKey;
    private Boolean isGeometry;
    private Boolean isTime;
    private Boolean isChinese;
    private String format;
    private int index;

    public ConstructedDataField(JSONObject field) {
        this.name = field.getString("name");
        this.text = field.getString("text");
        this.type = field.getString("type");
        this.length = field.getInt("length");
        this.description = field.getString("description");
        this.isKey = field.getBoolean("isKey");
        this.isGeometry = field.getBoolean("isGeometry");
        this.isTime = field.getBoolean("isTime");
        this.isChinese = field.getBoolean("isChinese");
        this.format = field.getString("format");
        this.index = field.getInt("index");
    }

    public ConstructedDataField() {
        this.name = "";
        this.text = "";
        setDefault();
    }

    public ConstructedDataField(String _name) {
        this.name = _name;
        this.text = _name;
        setDefault();
    }

    public ConstructedDataField(String _name, String _text) {
        this.name = _name;
        this.text = _text;
        setDefaultSpatial();
    }

    public JSONObject field2Json() {
        JSONObject json = new JSONObject();
        json.put("name", this.name);
        json.put("text", this.text);
        json.put("type", this.type);
        json.put("length", this.length);
        json.put("description", this.description);
        json.put("isKey", this.isKey);
        json.put("isGeometry", this.isGeometry);
        json.put("isTime", this.isTime);
        json.put("isChinese", this.isChinese);
        json.put("format", this.format);
        return json;
    }

    private void setDefault() {
        this.type = "String";
        this.length = 255;
        this.description = "Default";
        this.isKey = false;
        this.isGeometry = false;
        this.isTime = false;
        this.isChinese = false;
        this.format = "";
    }

    private void setDefaultSpatial() {
        this.type = "String";
        this.length = 2000;
        this.description = "DefaultSpatial";
        this.isKey = false;
        this.isGeometry = true;
        this.isTime = false;
        this.isChinese = false;
        this.format = "";
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof ConstructedDataField) {
            ConstructedDataField field = (ConstructedDataField) obj;
            return this.name.equals(field.name);
        }
        return false;
    }
}
